#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <stdio.h>
#include <ctype.h>

int main( int argc, char** argv )
{
  CvCapture	*capture = 0;
  IplImage	*frame 	 = 0;	// grabbed frame
    
  if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) {
    printf("capturing cam...\n");
    capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );

    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,320);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,240);

    // not supportet on my RPi:
    // cvSetCaptureProperty(capture, CV_CAP_PROP_FPS,5); 
  }
  else if( argc == 2 ) {
    printf("capturing video from %s...\n", argv[1]);
    capture = cvCaptureFromAVI( argv[1] );
  }
    
  if (!capture) {
    fprintf(stderr,"Could not initialize capturing...\n");
    return -1;
  }

  // capture window
  cvNamedWindow( "Capture", CV_WINDOW_AUTOSIZE );
  cvMoveWindow( "Capture", 0, 0 );

  while (1) {
    int key;
    
    // capture a frame
    frame = cvQueryFrame( capture );
    if (!frame)
      break;		    
    cvShowImage( "Capture", frame );

    key = cvWaitKey( 5 );
    if( key == 1048603 )
       break;	
  }

  cvReleaseCapture( &capture );
  cvDestroyWindow ("Capture" );

  return 0;
}
